package com.atguigu.gmall.activity.redis;

import com.atguigu.gmall.activity.util.CacheHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @author: atguigu
 * @create: 2023-01-30 15:19
 */
@Slf4j
@Component
public class MessageReceive {

    /**
     * 当前JVM实例监听Redis中 秒杀商品状态 话题,更新当前JVM本地缓存中商品状态
     *
     * @param state 形式:""19:1"" 需要将其中引号去掉  商品ID:状态
     *              状态1:可以买  状态0:售罄
     */
    public void receiveSeckillGoodState(String state) {
        if (StringUtils.isNotBlank(state)) {
            log.info("监听到秒杀状态消息:" + state);
            state = state.replace("\"", "");
            //将秒杀商品状态放入本地缓存中
            String[] split = state.split(":");
            if (split != null && split.length == 2) {
                CacheHelper.put(split[0], split[1]);
            }
        }
    }
}
